import 'package:changxindianzishangwu/dao/user_info_dao.dart';
import 'package:changxindianzishangwu/model/user_info_model.dart';
import 'package:changxindianzishangwu/util/toast.dart';
import 'package:changxindianzishangwu/widget/app_bar.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_callback.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_user_full_info.dart';
import 'package:tencent_im_sdk_plugin/tencent_im_sdk_plugin.dart';

import 'add_setting_page.dart';

class PrivacySettingPage extends StatefulWidget {
  PrivacySettingPage({Key key}) : super(key: key);

  @override
  _PrivacySettingPageState createState() => _PrivacySettingPageState();
}

class _PrivacySettingPageState extends State<PrivacySettingPage> {
  bool _lights = false;

  @override
  void initState() {
    super.initState();
    getDate();
  }

  getDate() async {
    await TencentImSDKPlugin.v2TIMManager.getLoginUser().then((value) {
      TencentImSDKPlugin.v2TIMManager
          .getUsersInfo(userIDList: [value.data]).then((value) {
        _lights = value.data[0].allowType == 1 ? true : false;
        setState(() {});
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: TopAppBar(context, "隐私设置"),
      body: Column(
        children: [
          Container(
            alignment: Alignment.center,
            color: Colors.white,
            margin: EdgeInsets.only(top: 10),
            child: Column(
              children: <Widget>[
                MergeSemantics(
                  child: ListTile(
                    title: Text(
                      '加我为好友时需要验证',
                      style: TextStyle(fontSize: ScreenUtil().setSp(15)),
                    ),
                    trailing: CupertinoSwitch(
                      activeColor: Color.fromRGBO(0, 220, 169, 1),
                      value: _lights,
                      onChanged: (bool value) async {
                        _updateAllowGreet(value);
                        V2TimCallback res =
                            await TencentImSDKPlugin.v2TIMManager.setSelfInfo(
                          userFullInfo: V2TimUserFullInfo.fromJson(
                            {
                              "allowType": value ? 1 : 0,
                            },
                          ),
                        );
                        if (res.code == 0) {
                          setState(() {
                            _lights = !_lights;
                          });
                          _updateUserInfo();
                        }
                      },
                    ),
                    onTap: () {
                      setState(() {
                        _lights = !_lights;
                      });
                    },
                  ),
                ),
                Divider(
                  height: 1,
                  indent: ScreenUtil().setWidth(10),
                  color: Color.fromRGBO(248, 248, 248, 1),
                  thickness: ScreenUtil().setHeight(1),
                ),
                // _addVoid(),
              ],
            ),
          ),
          // _hmd(),
        ],
      ),
    );
  }

  InkWell _addVoid() {
    return InkWell(
      onTap: () {
        //todo头像跳转
        Get.to(() => AddSettingPage());
      },
      child: ListTile(
        leading: Text(
          "添加我的方式",
          style: TextStyle(fontSize: ScreenUtil().setSp(15)),
        ),
        trailing: Icon(Icons.keyboard_arrow_right),
      ),
    );
  }

  ///黑名单 暂时隐藏 7.30
  InkWell _hmd() {
    return InkWell(
      onTap: () {},
      child: Container(
        color: Colors.white,
        margin: EdgeInsets.only(top: 10),
        alignment: Alignment.center,
        child: ListTile(
          leading: Text(
            "黑名单",
            style: TextStyle(fontSize: ScreenUtil().setSp(15)),
          ),
          title: Container(
            alignment: Alignment.centerRight,
            height: ScreenUtil().setHeight(50),
            width: ScreenUtil().setWidth(50),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(4),
            ),
          ),
          trailing: Icon(Icons.keyboard_arrow_right),
        ),
      ),
    );
  }

  void _updateAllowGreet(bool value) {
    // setState(() {
    //   _lights = value;
    // });
    // UserInfoDao.updateUserInfo("allowGreet", name)
  }

  _updateUserInfo() async {
    try {
      UserInfoModel userInfoModel =
      await UserInfoDao.updateUserSettings("friendsVerify", _lights?1:0);
      if (userInfoModel.resultCode == 0) {
        toast(userInfoModel.resultMsg);
        return;
      }
      // Navigator.of(context).pop();
      toast("修改成功");
    } catch (e) {
      print(e);
      // toast("修改失败");
    }
  }
}

